package com.examination.exam.controller;

import com.examination.domain.common.ResponseResult;
import com.examination.domain.exam.base.BaseEntity;
import com.examination.domain.exam.entity.Question;
import com.examination.domain.exam.vo.QuestionVo;
import com.examination.domain.utils.BeanCopyUtils;
import com.examination.enums.AppHttpCodeEnum;
import com.examination.exam.service.QuestionService;
import com.examination.exam.utils.MetaDataUtil;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/exam/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/get/{id}")
    public ResponseResult getQuestionById(@PathVariable String id) {
        Question question = questionService.getQuestionById(id);
        if (question.getBaseEntity().getIsDelete() != 1) {
            return ResponseResult.okResult(BeanCopyUtils.copyBean(question, QuestionVo.class));
        } else return ResponseResult.errorResult(AppHttpCodeEnum.QUESTION_NULL);
    }

    @GetMapping("/getByUser/{userId}")
    public ResponseResult getQuestionsByUser(@PathVariable String userId) {
        List<Question> questionsByUser = questionService.getQuestionByQuery(Filters.eq("baseEntity.createBy", userId));
        ArrayList<Question> questions = new ArrayList<>();
        for (Question question : questionsByUser) {
            if (!Objects.isNull(question.getBaseEntity().getIsDelete()) && question.getBaseEntity().getIsDelete() != 1)
                questions.add(question);
        }
        if (questions.size() > 0)
            return ResponseResult.okResult(BeanCopyUtils.copyBeanList(questions, QuestionVo.class));
        else
            return ResponseResult.errorResult(AppHttpCodeEnum.QUESTION_NULL);
    }

    @GetMapping("/getall")
    public ResponseResult getQuestions() {
        List<Question> allQuestion = questionService.getQuestionByQuery(null);
        ArrayList<Question> questions = new ArrayList<>();
        for (Question question : allQuestion) {
            if (!Objects.isNull(question.getBaseEntity().getIsDelete()) && question.getBaseEntity().getIsDelete() != 1)
                questions.add(question);
        }
        if (questions.size() > 0)
            return ResponseResult.okResult(BeanCopyUtils.copyBeanList(questions, QuestionVo.class));
        else
            return ResponseResult.errorResult(AppHttpCodeEnum.QUESTION_NULL);
    }

    @PostMapping("/add")
    public ResponseResult addQuestion(@RequestBody Question question) {
        BaseEntity baseEntity = new BaseEntity();
        question.setBaseEntity(baseEntity);
        MetaDataUtil.create(question);
        questionService.addQuestion(question);
        return ResponseResult.okResult();
    }

    @PostMapping("/update")
    public ResponseResult updateQuestion(@RequestBody Map updateMap) {
        List<Bson> updates = MetaDataUtil.update();
        for (Object o : updateMap.keySet())
            if (!String.valueOf(o).equals("id"))
                if (String.valueOf(o).equals("score"))
                    updates.add(Updates.set(String.valueOf(o), Double.valueOf(((Integer) updateMap.get(o)).toString())));
                else
                    updates.add(Updates.set(String.valueOf(o), updateMap.get(o)));

        Bson bson = Updates.combine(updates);
        long l = questionService.updateQuestion(Filters.eq("_id", new ObjectId(String.valueOf(updateMap.get("id")))), bson);
        return ResponseResult.okResult(l);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult delectQuestion(@PathVariable String id) {
        List<Bson> updates = MetaDataUtil.delete();
        Bson bson = Updates.combine(updates);
        long l = questionService.updateQuestion(Filters.eq("_id", new ObjectId(id)), bson);
        return ResponseResult.okResult(l);
    }

}
